﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Logging
{
    class TextFileLogAppender : ILogAppender
    {
        public TextFileLogAppender()
        {
            LoggerConfig config = LoggerConfig.GetInstance("TextFileLogAppender");
            string logFile = config.GetParameter("logfile");
            string nameTmp = logFile;

            int index = 0;
            while (File.Exists(nameTmp))
            {
                string name = Path.GetFileNameWithoutExtension(logFile);
                nameTmp = string.Format("{0}_{1}.log", name, index);
                index++;
            }

            m_Writer = File.CreateText(nameTmp);

        }

        public void Append(string text)
        {
            m_Writer.Write(text);
        }

        public void AppendLine(string text)
        {
            m_Writer.WriteLine(text);
            m_Writer.Flush();
        }

        TextWriter m_Writer;
    }
}
